﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-13 12:49:31
// 负责人：lvsc
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
using OF.Component.Enum;
using OF.Component.DTO;

namespace OF.Component.DAL
{

    /// <summary>
    ///网办订单 数据访问层
    /// </summary>	
    internal partial class NetworkOrder
    {

        /// <summary>
        /// 根据断言条件获取单个网办订单
        /// </summary>
        public NetworkOrderInfo QuerySingle(Expression<Func<NetworkOrderInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateObject(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 根据断言条件获获取网办订单列表
        /// </summary>
        public IList<NetworkOrderInfo> QueryList(Expression<Func<NetworkOrderInfo, bool>> predicate)
        {
            return SQLServerProvider.CreateList(ConnectionStringKey, predicate);
        }

        /// <summary>
        /// 获取用户id
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="bizTypeId"></param>
        /// <returns></returns>
        public long GetOrderId(int userId, int bizTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT OrderID FROM NetworkOrder WITH(NOLOCK) WHERE UserID=@UserID AND BizTypeID=@BizTypeID AND OrderState<@OrderState ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@UserID", DbType.Int32, userId);
            db.AddInParameter(dbc, "@BizTypeID", DbType.Int32, bizTypeId);
            db.AddInParameter(dbc, "@OrderState", DbType.Int32, (int)OrderStateEnum.Complete);
            object result = db.ExecuteScalar(dbc);
            return EConvert.ToLong(result);
        }

        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<NetworkOrderDto> SearchList(NetworkOrderCondition condition)
        {
            IList<NetworkOrderDto> list = new List<NetworkOrderDto>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_NetworkOrder_search]");
            //设置参数
            db.AddInParameter(dbc, "@OrderID", DbType.Int64, condition.OrderID);
            db.AddInParameter(dbc, "@ProvinceId", DbType.Int32, condition.ProvinceId);
            db.AddInParameter(dbc, "@CityId", DbType.Int32, condition.CityId);
            db.AddInParameter(dbc, "@MasterBizTypeId", DbType.Int32, condition.MasterBizTypeId);
            db.AddInParameter(dbc, "@BizTypeId", DbType.Int32, condition.BizTypeId);
            db.AddInParameter(dbc, "@StartDateTime", DbType.String, condition.StartDateTime);
            db.AddInParameter(dbc, "@EndDateTime", DbType.String, condition.EndDateTime);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    list.Add(SQLServerProvider.Create<NetworkOrderDto>(dr));
                }
            }
            condition.total = (int)db.GetParameterValue(dbc, "@OutCount");
            return list;
        }

        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public NetworkOrderDetailDto GetOrderInfo(long orderId)
        {
            NetworkOrderDetailDto info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_NetworkOrderInfo_search]");
            //设置参数
            db.AddInParameter(dbc, "@OrderID", DbType.Int64, orderId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = SQLServerProvider.Create<NetworkOrderDetailDto>(dr);
                }
            }
            return info;
        }

        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataTable GetOrderList(NetworkOrderApiCondition condition)
        {
            NetworkOrderDetailDto info = null;
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("[p_GetOrderList]");
            //设置参数
            db.AddInParameter(dbc, "@UserID", DbType.Int64, condition.UserID);
            db.AddInParameter(dbc, "@IsComplete", DbType.Int32, condition.IsComplete);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, condition.page);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, condition.rows);
            db.AddOutParameter(dbc, "@OutCount", DbType.Int32, 4);
            DataSet ds = db.ExecuteDataSet(dbc);
            return ds.Tables[0];
        }

        ///<summary>
        ///根据主键取出信息
        ///</summary>
        public DataTable GetDetailByID(long orderID)
        {
            Model.NetworkOrderInfo info = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT T.*,T1.BizTypeName,T2.HospitalName FROM NetworkOrder T WITH(NOLOCK) ");
            strSql.Append("LEFT JOIN BizType T1 WITH(NOLOCK) ON T1.BizTypeID = T.BizTypeID ");
            strSql.Append("LEFT JOIN Hospital T2 WITH(NOLOCK) ON T.HospitalID=T2.HospitalID ");
            strSql.Append("WHERE [OrderID]=@OrderID ");

            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbc, "@OrderID", DbType.Int64, orderID);
            DataSet ds = db.ExecuteDataSet(dbc);
            return ds.Tables[0];
        }
    }

}